package com.android.music.prolyric;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;




public class MusicPlayer extends Activity implements OnCompletionListener, SeekBar.OnSeekBarChangeListener{
	private ImageButton btnSearch, btnPlay,btnNext,btnPrevious,btnRepeat,btnShuff,btnPlaylist;
	private TextView tvSongName, tvSinger,tvSongCount,tvCurentSongTime, tvTotalSongTime;
	public static boolean changeLyric=false,searchPress=false;
	private SeekBar seekBar;
	private ProgressBar progress ;
	private String [] lyrics=null;
	private RelativeLayout boxLayout;
	private ListView lvLyric;
	
	String songTitle;
	
	
	private Handler mHandler = new Handler();
	private Utilities utils;
	
	private Intent audioServiceIntent;
	private Intent saveLyricServiceIntent;
	
	private boolean mBoud = false;
	
	private IntentFilter intentfilterSetting;
	private IntentFilter intentfilter;
	private IntentFilter songInforIntentFilter;
	private IntentFilter noMusicIntentFiler ;
	private AudioService baService = new AudioService();
	private int en=0;
	/*------------------------------Dialog------------------------------*/
	private AlertDialog.Builder dialog_search_builder;
	private AlertDialog dialog_search;
	private ListView lv_dialog;
	private EditText et_dialog;
	private TextView tv_dialog;
	private ImageButton imgbtn_dialog;
	private ProgressBar processSearch_dialog;	
	private String[] song;
	private ArrayList<HashMap<String, String>> listLyricUrl = new ArrayList<HashMap<String,String>>();
	
	private int n;
	private String[] currentSongInfo;
	private SharedPreferences prefs;
	
	private ArrayList<String> str;
	private ArrayAdapter<String > arrayAdapter;
	private listAdapter listAdap;
	int colorSong,colorSinger,colorId,colorLyric,colorTime,imgBgr;
	int defaultColor = 0xffffffff;
	
	
	/*-------------------------------End Dialog-------------------------*/
	
	
	private ServiceConnection serviceConnection = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			Log.d("Main Disconnect", "disconnect");
			mBoud =false;
			//baService = null;
			
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d("Main Connection", "connectied");
			baService = ((AudioService.AudioServiceBinder)service).getService();			
			mBoud = true;
			startService(audioServiceIntent);			
		}
	};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
       
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //get component of UI
        progress = (ProgressBar)findViewById(R.id.progressBar);
        progress.setVisibility(8);
        btnSearch = (ImageButton)findViewById(R.id.btnSearch);
        btnPlay = (ImageButton)findViewById(R.id.btnPlay);
        btnPlay.setImageResource(R.layout.pause_selector);
        btnNext = (ImageButton)findViewById(R.id.btnNext);
        btnPrevious = (ImageButton)findViewById(R.id.btnPrev);
        btnRepeat = (ImageButton)findViewById(R.id.btnRepeat);
        btnShuff = (ImageButton)findViewById(R.id.btnShuffe);
        btnPlaylist = (ImageButton)findViewById(R.id.btnPlaylist);
        tvSongName = (TextView)findViewById(R.id.tvNameSong);
        tvSinger = (TextView)findViewById(R.id.tvSinger);
        tvSongCount = (TextView)findViewById(R.id.tvSongCount);
        tvCurentSongTime = (TextView)findViewById(R.id.tvSongCurrentTime);
        tvTotalSongTime  = (TextView)findViewById(R.id.tvSongTotalTime);
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        lvLyric = (ListView) findViewById(R.id.lvLyric);
        lvLyric.setLongClickable(false);
        
        boxLayout = (RelativeLayout)findViewById(R.id.box_layout);        
        prefs = getSharedPreferences("playerColor", MODE_PRIVATE);
        
        
        
        audioServiceIntent = new Intent(MusicPlayer.this,AudioService.class);
        saveLyricServiceIntent = new Intent(this,SaveLyricService.class);
        str= new ArrayList<String>();
        str.add("");
        setColor();
        
        lvLyric.setOnItemClickListener(new OnItemClickListener() 
        {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
			{
				if(lyrics!=null)
				{
					if(en>0)
						display(lyrics[--en]);
					else
						display(lyrics[++en]);
				}
				
			}
		});
        
        
        
        

        /*---------------------set dialog---------------------------------------------*/
        dialog_search_builder = new AlertDialog.Builder(this);
        dialog_search_builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
	        {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
					progress.setVisibility(8);
					if(searchPress || (!searchPress && !changeLyric))
						{
							searchPress=false;
							display("Lyric not found");
						}
				}
			});
        View view = getLayoutInflater().inflate(R.layout.search_dialog_, null);
        lv_dialog = (ListView)view.findViewById(R.id.lvDialogResult);
		et_dialog = (EditText)view.findViewById(R.id.etDialogSearch);
		tv_dialog = (TextView)view.findViewById(R.id.tvDialogCount);
		imgbtn_dialog =(ImageButton)view.findViewById(R.id.btnDialogSearch);
		processSearch_dialog = (ProgressBar)view.findViewById(R.id.progressBarDialog);
		processSearch_dialog.setVisibility(8);

		
		dialog_search_builder.setView(view);
		
		/*
		 * Bat dau chay Service 
		 * chay lan dau, type start = 0
		 * */
		/*audioServiceIntent = new Intent(this,AudioService.class);
	    Bundle b = new Bundle(); 
	    b.putInt("type_start", 0);
	    audioServiceIntent.putExtras(b);
	    startService(audioServiceIntent);*/
	    // baService.pauseSong();
	      
	    /*****************************/
	    
	    
	    //DatabaseManger.getAcountOnHost();
		
		imgbtn_dialog.setOnClickListener(new OnClickListener() 
		{
			
			public void onClick(View v) 
			{
				searchPress=true;
				processSearch_dialog.setVisibility(0);
				tv_dialog.setText("Searching...");
				lv_dialog.setAdapter(null);
				String s = et_dialog.getText().toString();
				Intent i = new Intent(MusicPlayer.this,LoadLyricService.class);								
				i.putExtra("songname",s);
				i.putExtra("lyricID", baService.lyricID+"");
				startService(i);
				//gui ten bai hat len tim kiem
			}
		});
				
		lv_dialog.setOnItemClickListener(new OnItemClickListener() 
		{

			public void onItemClick(AdapterView<?> arg0,View v, int position, long id) 
			{
				//Intent i = new Intent(MusicPlayer.this,LoadLyricService.class);		
				Bundle bundle = new Bundle();
				bundle.putString("urlSong",listLyricUrl.get(position).get("urlSong"));
				lvLyric.setVisibility(View.GONE);
				baService.startServiceFromGUI(bundle);
				((DialogInterface) dialog_search).dismiss();
				progress.setVisibility(0);
			}
		});
        
		dialog_search = dialog_search_builder.create();
        /*----------------------------------------------*/       
        
        
        
      
        
        //tvLyric.setVisibility(View.GONE);
		//progress.setVisibility(View.VISIBLE);
        
        
        
       
        
        utils = new Utilities();    
      // GlobalState gs = (GlobalState) getApplication();
        
       seekBar.setOnSeekBarChangeListener(this); // Important
		
        
        //register the receiver;
       intentfilterSetting = new IntentFilter();
       intentfilterSetting.addAction("LOADSETTING");
       registerReceiver(intentSettingReceiver, intentfilterSetting);
       
        intentfilter = new IntentFilter();
        intentfilter.addAction("lOAD_LYRIC_FROM_WEB");
        registerReceiver(intentReceiver, intentfilter);
        songInforIntentFilter = new IntentFilter();
        songInforIntentFilter.addAction("GET_SONG_INFO");
        registerReceiver(songInfoReceiver, songInforIntentFilter);
        
        noMusicIntentFiler = new IntentFilter();
        noMusicIntentFiler.addAction("ERROR_NO_MUSIC");
        registerReceiver(noMusicReceiver, noMusicIntentFiler);
		       
		
        btnSearch.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			{
				
				Intent i = new Intent(MusicPlayer.this,SearchActivity.class);
				//listSongs = new ArrayList<Song>();
				//Song s = new Song("a","b");
				//i.putParcelableArrayListExtra("song", s);
				//i.putParcelableArrayListExtra(name, value)
				startActivityForResult(i, 200);
				//showDialog(0);
			}
		});
        
        btnPlaylist.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {

				Intent playlistIntent = new Intent(MusicPlayer.this, Library.class);
				startActivityForResult(playlistIntent, 100);
				
				
			}
		});
        
        
        
        
        btnPlay.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				if(baService.switchPlayPause())
					btnPlay.setImageResource(R.layout.pause_selector);					
				else 
					btnPlay.setImageResource(R.layout.play_selector);
								
					
						
			}
		});
        
        btnNext.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				lvLyric.setVisibility(View.GONE);
				progress.setVisibility(View.VISIBLE);
				baService.NextSong();
				
				//updateProgressBar();
				
			}
		});
        
        
        btnPrevious.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				lvLyric.setVisibility(View.GONE);
				progress.setVisibility(View.VISIBLE);
				baService.PreviousSong();
				
				//updateProgressBar();
			}
		});
        
        btnRepeat.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(baService.getRepeat()){
					Toast.makeText(getBaseContext(), "Turn off repeat song", Toast.LENGTH_SHORT).show();
					baService.setRepeat(false);				
					btnRepeat.setImageResource(R.layout.repeat_selector);
				}else{
					Toast.makeText(getBaseContext(), "Repeat current song", Toast.LENGTH_SHORT).show();
					// make repeat to true
					baService.setRepeat(true);
					
					baService.setShuffe(false);
					btnRepeat.setImageResource(R.layout.repeat_selector_focused);
					btnShuff.setImageResource(R.layout.shuffle_selector);
				}	
				
			}
		});
        
        
        btnShuff.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				if(baService.getShuffe()){
					baService.setShuffe(false);
					Toast.makeText(getBaseContext(), "Turn off shuffe", Toast.LENGTH_LONG).show();
					btnShuff.setImageResource(R.layout.shuffle_selector);
				}else{
					// make repeat to true
					baService.setShuffe(true);
					Toast.makeText(getBaseContext(), "Turn on shuffe", Toast.LENGTH_LONG).show();
					baService.setRepeat(false);
					btnShuff.setImageResource(R.layout.shuffle_selector_focused);
					btnRepeat.setImageResource(R.layout.repeat_selector);
				}	
			}
		});

    }
    
    
    
    @Override
    protected void onStart() {
    	// TODO Auto-generated method stub
    	super.onStart();   

	    Bundle b = new Bundle(); 
	    b.putInt("type_start", 0);
	    audioServiceIntent.putExtras(b);
		bindService(audioServiceIntent, serviceConnection, BIND_AUTO_CREATE);
    }
    
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
    	getMenuInflater().inflate(R.menu.activity_music_player, menu);        
        return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    	
    	switch (item.getItemId()) 
    	{
			case R.id.menu_change_lyric:
				if(!AudioService.online)
				{
					changeLyric=true;
					et_dialog.setText(songTitle);
					if(!dialog_search.isShowing())
						dialog_search.show();
				}
				return true;
			case R.id.menu_change_bg :
				//Toast.makeText(getBaseContext(), "change background", Toast.LENGTH_SHORT).show();
				//Intent i = new Intent(MusicPlayer.this, ChangeBackground.class);
				Intent i = new Intent(MusicPlayer.this, settings.class);
				startActivity(i);
			
				return true;					
		}
    	
    	return super.onOptionsItemSelected(item);
    
    	
    }
    
        
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if(requestCode == 100)
    	{
    		Log.d("resultCode", ""+resultCode+" "+RESULT_OK);
    		
    		
    		if(resultCode == RESULT_OK)
    		{
    			Log.d("activity result", "start service");
				GlobalState g = (GlobalState)getApplicationContext();
				int index = g.getSongIndex();
				Log.d("songindex", index+"");
				
				
				Bundle b = new Bundle();
			    b.putInt("type_start", 1);
			    audioServiceIntent.putExtras(b);
				startService(audioServiceIntent);
				btnPlay.setImageResource(R.layout.play_selector);
				lvLyric.setVisibility(View.GONE);
				progress.setVisibility(View.VISIBLE);
    		} 
    			
    		
    			
    	
    	}
    	
    }

    /**
	 * Function to play a song
	 * @param songIndex - index of song
	 * */
	public void playSong()
	{
		lvLyric.setVisibility(View.GONE);
		progress.setVisibility(View.VISIBLE);
		btnPlay.setImageResource(R.layout.play_selector);
		baService.playSong();
		updateProgressBar();
	}
	
	/**
	 * Update timer on seekbar
	 * */
	public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);        
    }	
	
	/**
	 * Background Runnable thread
	 * */
	private Runnable mUpdateTimeTask = new Runnable() { 
		   public void run() {
			  
			   if(!MusicPlayer.this.isFinishing())
			   {
				   long totalDuration = baService.gettotalDuration();
				   long currentDuration = baService.getcurrentDuration();
				  // Log.e("Update Time Task", ""+totalDuration);
				  
				   // Displaying Total Duration time
				   tvTotalSongTime.setText(""+utils.milliSecondsToTimer(totalDuration));
				   // Displaying time completed playing
				   tvCurentSongTime.setText(""+utils.milliSecondsToTimer(currentDuration));
				   
				   // Updating progress bar
				   int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
				   //Log.d("Progress", ""+progress);
				   seekBar.setProgress(progress);
				   
				   // Running this thread after 100 milliseconds
			      
				   mHandler.postDelayed(this, 100);
			   }
		   }
		};   
		 
	/**
	 * 
	 * */
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
		
	}

	/**
	 * When user starts moving the progress handler
	 * */
	public void onStartTrackingTouch(SeekBar seekBar) {
		// remove message Handler from updating progress bar
		mHandler.removeCallbacks(mUpdateTimeTask);
    }
	
	/**
	 * When user stops moving the progress hanlder
	 * */
	public void onStopTrackingTouch(SeekBar seekBar) {
		mHandler.removeCallbacks(mUpdateTimeTask);
		int totalDuration = baService.gettotalDuration();
		int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
		
		// forward or backward to certain seconds
		//mp.seekTo(currentPosition);
		baService.seekTo(currentPosition);
		
		// update timer progress again
		updateProgressBar();
    }

	/**
	 * On Song Playing completed
	 * if repeat is ON play same song again
	 * if shuffle is ON play random song
	 * */
	public void onCompletion(MediaPlayer arg0) {
		
		
	}
	
	@Override
	 public void onDestroy(){
	 super.onDestroy();
	 if(mBoud)
	 {
		 if(!baService.getStateMP())
		 {
			 Log.d("mBound", ""+mBoud);
			
				 unbindService(serviceConnection);
				 stopService(audioServiceIntent);
				 mBoud = false;
		
			 
		 }
		 else
		 
		 unbindService(serviceConnection);
	 }
	 unregisterReceiver(intentSettingReceiver);
	 unregisterReceiver(intentReceiver);
	 unregisterReceiver(songInfoReceiver);
	 unregisterReceiver(noMusicReceiver);
	   
	 }
	
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		 
	}
	
	private void setColor()
	{
		colorSong= (int) prefs.getInt("songcolor",defaultColor);
		colorSinger= (int) prefs.getInt("singercolor",defaultColor);
		colorId=(int) prefs.getInt("idcolor",defaultColor);
		colorLyric= (int) prefs.getInt("lyriccolor",defaultColor);
		colorTime=(int) prefs.getInt("timecolor",defaultColor);
		imgBgr = (int) prefs.getInt("pbgr",R.drawable.lib2);
		
		tvSongName.setTextColor(colorSong);
		tvSinger.setTextColor(colorSinger);
		tvSongCount.setTextColor(colorId);
		tvCurentSongTime.setTextColor(colorTime);
		tvTotalSongTime.setTextColor(colorTime);
		boxLayout.setBackgroundDrawable(getResources().getDrawable(imgBgr));
		//listAdap = new listAdapter(this, str,getResources().getColor(ChangeBackground.textColors[currentBackground]));
		listAdap = new listAdapter(this, str,colorLyric);
		lvLyric.setAdapter(listAdap); 
		
	}
	
	private BroadcastReceiver intentSettingReceiver = new BroadcastReceiver()
	{

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			Bundle b =intent.getExtras();
			if(b!=null)
			{
				if(b.getString("playerSetting")!=null)
				{
					setColor();
				}
			}
		}
		
	};
	
	private BroadcastReceiver songInfoReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle b = intent.getExtras();
			lvLyric.setVisibility(8);
			if(b!=null)
			{
				currentSongInfo= b.getStringArray("songInfors");
				
				if(currentSongInfo!=null)
				{
					if(dialog_search.isShowing())
						dialog_search.dismiss();
					tvSongName.setText(currentSongInfo[0].trim());
					songTitle = currentSongInfo[0].trim();
					tvSinger.setText(currentSongInfo[1]);
					tvSongCount.setText(currentSongInfo[2]);
					btnPlay.setImageResource(R.layout.play_selector);
					progress.setVisibility(View.VISIBLE);
					//tvLyric.setVisibility(View.INVISIBLE);
					updateProgressBar();
					boolean setPauseSong = b.getBoolean("setPauseSong");
					Log.d("setPausong", ""+setPauseSong);
					if(setPauseSong)
						btnPlay.setImageResource(R.layout.pause_selector);
					
				}
			}
			
		}
	};
	
	private BroadcastReceiver noMusicReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			
			//new Dialog(this).setTitle("Please insert music file to sdcard");
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
			alertDialogBuilder.setTitle("No Music");
			alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					finish();
					
				}
			});
			
			alertDialogBuilder.show();
			
		}
	};
	
	private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle b = intent.getExtras();
			if(b!= null)
			{
				int lyricID = b.getInt("lyricID");				
				int localSearch = b.getInt("localSearch");
				ArrayList<HashMap<String, String>> lyricResult = (ArrayList<HashMap<String, String>>) b.getSerializable("lyricLoader");
				if(lyricResult != null)
		 		{
					
					
					if(lyricResult.size() == 1)
					{
							if(dialog_search.isShowing())
								dialog_search.dismiss();
							lyrics = new String[2];
							Bundle saveBunle = new Bundle();
							saveBunle.putString("singerLocal", currentSongInfo[1]);
							saveBunle.putInt("localSearch", localSearch);
														
							//neu nghe online lyricResult.get(0).size=2
							int n= lyricResult.get(0).size();
							if(n==2 || changeLyric==true)
							{
								//nghe online
								HashMap<String, String> dataToSave = new HashMap<String, String>();
								dataToSave.put("songname", currentSongInfo[0]);
								dataToSave.put("singer", currentSongInfo[1]);
								dataToSave.put("lyricEn", lyricResult.get(0).get("lyricEn"));
								dataToSave.put("lyricVi", lyricResult.get(0).get("lyricVi"));
								saveBunle.putSerializable("lyricToSave", dataToSave);	
								Log.i("update", "dang doi ket qua");
							}
							
							else
								saveBunle.putSerializable("lyricToSave", lyricResult.get(0));
							
							saveLyricServiceIntent.putExtras(saveBunle);
							startService(saveLyricServiceIntent);
							
							if(baService.lyricID == lyricID)
							{
								lyrics[0] = lyricResult.get(0).get("lyricEn");
								lyrics[1] = lyricResult.get(0).get("lyricVi");
								if(dialog_search.isShowing())
									dialog_search.dismiss();
								display(lyrics[en]);
								
							}				
							
							
					}					
				
				
					else
					{
						
						processSearch_dialog.setVisibility(8);
						listLyricUrl = lyricResult;
						SimpleAdapter adapter = 
								new SimpleAdapter(MusicPlayer.this, lyricResult, R.layout.simple_list_item, new String[]{"songname_singer"}, new int[]{R.id.tv_dialog_item_list});
						lv_dialog.setAdapter(adapter);
						et_dialog.setText(songTitle);
						tv_dialog.setText(lyricResult.size() + " lyrics found");
						
						if(baService.lyricID == lyricID && !dialog_search.isShowing())
							{
								progress.setVisibility(8);
								dialog_search.show();
							}
													
					}
			 	}
				
				else 
				{
					Log.d("lyricLoader", "null");	
					display("Lyric not found");
					processSearch_dialog.setVisibility(8);					
					tv_dialog.setText("0 lyrics found");
					et_dialog.setText(songTitle);
					lv_dialog.setAdapter(null);
						
					if(dialog_search.isShowing())
						dialog_search.dismiss();				
											
				}
				
			}
		}
			
		
	};

		
	private void display(String s)
	{
		try
		{
			progress.setVisibility(8);
			str = new ArrayList<String>();
			InputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
	    	BufferedReader reader = new BufferedReader(new InputStreamReader(in));
	    	String line="";
	    	while((line= reader.readLine())!=null)
	    		str.add(line);
	    	
	    	listAdap = new listAdapter(this, str,colorLyric);
    		
			lvLyric.setAdapter(listAdap);
			lvLyric.setVisibility(0);
			
	    	reader.close();
	    	in.close();
		}
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch(id)
		{
			case 0: return dialog_search;
			
		}
		
		return null;
	}
	
	
	//check for internet
	
	
	
}
